//Copyright (c) 2024 Deng Zicheng
//        "CarController" is licensed under Mulan PSL v2.
//        You can use this software according to the terms and conditions of the Mulan PSL v2.
//        You may obtain a copy of Mulan PSL v2 at:
//        http://license.coscl.org.cn/MulanPSL2
//        THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
//        See the Mulan PSL v2 for more details.
package com.sinan.myapplication;

import java.util.Base64;
import java.util.UUID;


/**
 * 待设备处理信息管理对象
 */
public class AksCopy {
    private String value="";
    private boolean copy=true;
    private String uuid="";

    private int uuidSize=4;

    private boolean allow=false;

    public AksCopy() {
    }

    public AksCopy(int uuidSize) {
        if (uuidSize>10) {
            uuidSize=10;
        }
        this.uuidSize=uuidSize;
    }

    /**
     * 返回uuid是否相同，将应答标志置为true
     * @param uuid 获取到的uuid
     * @return 相同返回true反之false
     */
    public boolean copyEqualThenClearUUID(String uuid){
        if (this.uuid.equals(uuid)){
            this.copy=true;
            return true;
        }
        return false;
    }

    /**
     * 获取待应答值
     * @return 代待应答值
     */
    public String getValue() {
        return "<"+this.value+this.uuid+">";
    }

    /**
     * 设置应答值
     * @param value 应答值
     * @return 成功返回true，反之返回false
     */
    public boolean setValue(String value) {
        if (!this.value.equals(value)||this.allow) {
            this.uuid =this.getUUID();
            this.value = value;
            this.copy=false;
            return true;
        }
        return false;
    }

    /**
     * 是否允许设置相同应答值
     * @param k 是否应答值
     */
    public void allowAcceptSameVale(boolean k){
        this.allow=k;
    }

    /**
     * 是否应答
     * @return 如果没有应答返回true反之false
     */
    public boolean isNotCopy() {
        return !copy;
    }

    public void setCopied() {
        this.copy = true;
    }

    /**
     * 重置应答
     */
    public void setRecopy(){
        this.copy=false;
    }


    /**
     * 获取uuid字符
     * @return 返回指定长度的uuid字符串
     */
    private String getUUID() {
        UUID uuid = UUID.randomUUID();
        String substring = Base64.getEncoder().encodeToString(uuid.toString().getBytes());
        substring = substring.replace("#", "");
        substring = substring.replace(">", "");
        substring = substring.replace("<", "").substring(0, this.uuidSize);
        return substring;
    }
}
